# Prepares from Austrian Election Study Data 

# Required Packages
library(foreign)
library(dummies)

# Data
data <- read.dta("Oesterreich.dta",convert.factors=FALSE)

# Select Variables
varnames <- matrix(c(
                     "v6_1","wahl1.1",
                     "v6_2","wahl1.2",
                     "v6_3","wahl1.3",
                     "v10_1","wahl2",
                     "v10_2","wahl3",
                     "v10_3","wahl4",
                     "v10_4","wahl5",
                     "v11_1","rat.1",
                     "v11_2","rat.2",
                     "v11_3","rat.3",
                     "v11_4","rat.4",
                     "v11_5","rat.5",
                     "v11_6","rat.6",
                     "v15_1","reg.1",
                     "v15_2","reg.2",
                     "v15_3","reg.3",
                     "v15_4","reg.4",
                     "v15_5","reg.5",
                     "v15_6","reg.6",                     
                     "v25","lr.s",
                     "v26_1","lr.1",
                     "v26_2","lr.2",
                     "v26_3","lr.3",
                     "v26_4","lr.4",
                     "v26_5","lr.5",
                     "v26_6","lr.6",
                     "v17_2","rat.ovp.fpo",
                     "v17_6","rat.spo.gruene",
                     "v17_5","rat.ovp.gruene",
                     "v38","birth",
                     "v39","gender",
                     "v40","educ",
                     "v47","relig",
                     "v49","union",
                     "v50","PID",
                     "v55","income",
                      "v1","int",
                     "v31","I1",
                     "v32","I2",
                     "v33","I3")
                   , ncol=2, byrow=TRUE)

oest <- data[,varnames[,1]]
names(oest) <- varnames[,2]

##########
# Modification
##########

# Wahl
oest$wahl1 <- oest$wahl1.2
oest <- oest[,-c(1,2,3)]

foo <- grep("wahl",names(oest))
oest[,foo][oest[,foo]==97]<-0 #not voted
oest[,foo][oest[,foo]==99]<- NA

# lr Position
foo <- grep("lr",names(oest))
oest[,foo][oest[,foo]>11]<-NA

# Saklaometer
foo <- grep("rat",names(oest))
oest[,foo][oest[,foo]>11]<-NA

# Regierungsbterieliugun
foo <- grep("reg",names(oest))
oest[,foo][oest[,foo]>11]<-NA


# birth
oest$age <- 2010 - oest$birth 

# gender: 1 female
oest$gender <- oest$gender -1

# educ
oest$educ[oest$educ>7] <- NA

# relig: kath 
oest$relig[oest$relig>1 & oest$relig<8] <- 0 
oest$relig[oest$relig>1] <- NA

# union
oest$union[oest$union>4] <- NA
oest$union[oest$union==4] <- 0
oest$union[oest$union>0] <- 1

# PID
oest$PID[oest$PID>9] <- NA
oest$PID[oest$PID>6] <- 0

PID <- dummy(oest$PID)
oest <- cbind(oest,PID[,2:7])

# income
oest$income[oest$income>9] <- NA

# Interesst and Knowledge
oest$int[oest$int>=8] <- NA

# Interest
oest$I1[oest$I1==999] <- NA
oest$I1[!(oest$I1 %in% c(4,5,6))] <- 0
oest$I1[oest$I1!=0] <- 1

oest$I2[oest$I2==9] <- NA
oest$I2[oest$I2!=4] <- 0
oest$I2[oest$I2==4] <- 1

oest$I3[oest$I3==9] <- NA
oest$I3[!(oest$I3 %in% c(1,3)) ] <- 0
oest$I3[oest$I3>0] <- 1


save(oest,file="oest_long.Rdata")


# reshape, so that vignett are rowise binded
oest <- reshape(oest
                ,direction="long"
                ,varying=c("wahl1","wahl2","wahl3","wahl4","wahl5")
                ,sep="")


# Save Dataset
save(oest,file="oest.Rdata")


